package dcc.component;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import dcc.mbg.model.DictionaryItem;
import dcc.tools.BeanFactory;

public class JDictionaryPanel extends JPanel {
	private JDictionaryList keyList;
	private JDictionaryItemTable table;
	private JTextField tfValue;

	public JDictionaryPanel() {
		init();
	}

	private void init() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 500, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 388, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 1;
		add(panel, gbc_panel);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel rightPanel = new JPanel();
		panel.add(rightPanel, BorderLayout.CENTER);
		rightPanel.setLayout(new BorderLayout(0, 0));

		JPanel topPanel = new JPanel();
		topPanel.setBackground(Color.WHITE);
		topPanel.setPreferredSize(new Dimension(0, 32));
		rightPanel.add(topPanel, BorderLayout.NORTH);
		GridBagLayout gbl_topPanel = new GridBagLayout();
		gbl_topPanel.columnWidths = new int[] { 54, 310, 50, 50, 0 };
		gbl_topPanel.rowHeights = new int[] { 0, 0 };
		gbl_topPanel.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_topPanel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		topPanel.setLayout(gbl_topPanel);

		JLabel lblValue = new JLabel("\u503C:");
		lblValue.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));
		GridBagConstraints gbc_lblValue = new GridBagConstraints();
		gbc_lblValue.anchor = GridBagConstraints.EAST;
		gbc_lblValue.insets = new Insets(0, 0, 0, 5);
		gbc_lblValue.gridx = 0;
		gbc_lblValue.gridy = 0;
		topPanel.add(lblValue, gbc_lblValue);

		tfValue = new JTextField();
		GridBagConstraints gbc_tfValue = new GridBagConstraints();
		gbc_tfValue.insets = new Insets(0, 0, 0, 5);
		gbc_tfValue.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfValue.gridx = 1;
		gbc_tfValue.gridy = 0;
		topPanel.add(tfValue, gbc_tfValue);
		tfValue.setColumns(10);

		JButton btnAdd = new JButton("\u65B0\u589E");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				add();
			}
		});
		btnAdd.setBackground(UIManager.getColor("Button.background"));
		GridBagConstraints gbc_btnAdd = new GridBagConstraints();
		gbc_btnAdd.fill = GridBagConstraints.BOTH;
		gbc_btnAdd.insets = new Insets(0, 0, 0, 5);
		gbc_btnAdd.gridx = 2;
		gbc_btnAdd.gridy = 0;
		topPanel.add(btnAdd, gbc_btnAdd);

		JButton btnSave = new JButton("\u6DFB\u52A0");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				save();
			}
		});
		btnSave.setText("\u4FDD\u5B58");
		btnSave.setBackground(UIManager.getColor("Button.background"));
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.BOTH;
		gbc_btnSave.gridx = 3;
		gbc_btnSave.gridy = 0;
		topPanel.add(btnSave, gbc_btnSave);

		JScrollPane scrollPane = new JScrollPane();
		rightPanel.add(scrollPane, BorderLayout.CENTER);

		table = new JDictionaryItemTable(this);
		scrollPane.setViewportView(table);

		JScrollPane leftScrollPane = new JScrollPane();
		leftScrollPane.setPreferredSize(new Dimension(160, 2));
		panel.add(leftScrollPane, BorderLayout.WEST);
		keyList = new JDictionaryList(this, table);
		leftScrollPane.setViewportView(keyList);
	}

	private void add() {
		this.tfValue.setText("");
		this.tfValue.requestFocus();
		createNewItem(this.item.getParent());
	}

	private void save() {
		if (!this.tfValue.getText().equals("")) {
			item.setValue(this.tfValue.getText());
			// TODO save value;
//			((DictionaryService) BeanFactory.getBean("dictionaryService"))
//					.saveItem(item);
			add();
			keyList.updateTable();
		}
	}

	public void update() {
		keyList.update();
	}

	private DictionaryItem item;

	public void setValue(DictionaryItem item) {
		if (item != null) {
			this.item = item;
			this.tfValue.setText(item.getValue());
		}
	}

	public void createNewItem(int id) {
//		this.item = new DictionaryItem(id);
	}
}
